goog.provide('czk.ui.ZKComponent');

goog.require('goog.ui.Component');

/**
 * @extends {goog.ui.Component}
 * @constructor
 */
czk.ui.ZKComponent = function() {
  goog.base(this);
  this.blockZKChildrenAdd = false;
  this.hasZKChildren = true;
};
goog.inherits(czk.ui.ZKComponent, goog.ui.Component);

czk.ui.ZKComponent.prototype.blockZKChildrenAdd = false;

czk.ui.ZKComponent.prototype.hasZKChildren = true;

czk.ui.ZKComponent.prototype.pre = false;

czk.ui.ZKComponent.prototype.position = 0;

czk.ui.ZKComponent.prototype.initialize =
    function(name, parameter, portlet, children) {
      this.name = name;
      this.portlet = portlet;
      if (parameter) {
        if (parameter['label'])
          this.label = parameter['label'];
        if (parameter['sclass'])
          this.className = parameter['sclass'];
        if (parameter['_value'])
          this.value = parameter['_value'];
        if (parameter['pre']) {
          this.pre = parameter['pre'];
        }
      }
    };

czk.ui.ZKComponent.prototype.addZKChild = function(child, position) {

  var comp = null;
  try {
    comp = eval(child[0] + '()');
  } catch (e) {
    throw Error(goog.ui.Component.Error.STATE_INVALID);
  }
  if (comp == undefined)
    throw Error(goog.ui.Component.Error.STATE_INVALID);
  comp.position = position;
  comp.initialize(child[1], child[2], this.portlet, child[3]);
  if (!comp.addZKChildToElement()) {
    this.addChild(comp, true);
  } else {
    this.addChild(comp, false);
    comp.render(this.getElement());
  }
  if (!comp.blockZKChildrenAdd && child[3]) {
    goog.array.forEach(child[3], this.addZKChild, comp);
  }
};

czk.ui.ZKComponent.prototype.addZKChildToElement = function() {
  return false;
};

czk.ui.ZKComponent.prototype.getPreItems = function(preItems) {
  if (this.pre) {
    this.pre = false;
    goog.array.insert(preItems, this.name);
  }
  this.forEachChild(function(child) {
    child.getPreItems(preItems);
  });
};

czk.ui.ZKComponent.prototype.handleZKResponse = function(response) {

  this.forEachChild(function(child) {
    if (child.hasZKChildren)
      child.handleZKResponse(response);
  });

  var commands = response['rs'];

  goog.array.forEach(commands, function(c) {
    if (c[1][0] == this.name)
      this.handleZKResponseCommand(c);
  }, this);
};

czk.ui.ZKComponent.prototype.handleZKResponseCommand = function(command) {
  // general handling
  if (command[0] == 'setAttr') {
    var attr = command[1][1];
    var val = command[1][2];
    if (attr == '_value')
      this.value = val;
    else if (attr == 'value')
      this.value = val;
    if (attr = 'sclass')
      this.className = val;
    if (attr = 'label')
      this.label = val;
  }
  if (command[0] == 'addAft' || command[0] == 'addChd') {
    goog.array.forEach(command[1][1], this.addZKChild, this);
  }
};

czk.ui.ZKComponent.prototype.name = null;
czk.ui.ZKComponent.prototype.className = '';
czk.ui.ZKComponent.prototype.label = '';
czk.ui.ZKComponent.prototype.value = '';
czk.ui.ZKComponent.prototype.portlet = '';
